/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.common.systemmessage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.common.core.page.BasePageRequest;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.utils.DateUtil;
import com.icinfo.cloud.provider.punish.common.systemmessage.dto.SystemMessageIdRequestDto;
import com.icinfo.cloud.provider.punish.common.systemmessage.dto.SystemMessageInsertDto;
import com.icinfo.cloud.provider.punish.common.systemmessage.mapper.CaseSystemMessageMapper;
import com.icinfo.cloud.provider.punish.common.systemmessage.model.CaseSystemMessage;
import com.icinfo.cloud.provider.punish.common.systemmessage.service.ICaseSystemMessageService;
import com.icinfo.cloud.provider.punish.common.systemmessage.vo.SystemMessageListInfoVo;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import com.icinfo.framework.tools.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 描述:  case_system_message 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年12月09日
 */
@Service
@Slf4j
public class CaseSystemMessageServiceImpl extends MyBatisServiceSupport implements ICaseSystemMessageService {
    @Resource
    private CaseSystemMessageMapper caseSystemMessageMapper;

    /**
     * 查询消息提醒列表
     *
     * @param pageQueryDto 消息提醒列表结果 VO
     * @return java.util.List<com.icinfo.cloud.provider.punish.common.systemmessage.vo.SystemMessageListInfoVo>
     * @author yangsizhen
     * @date 2022/12/09
     */
    @Override
    public List<SystemMessageListInfoVo> getSystemMessageList(BasePageRequest pageQueryDto) {
        List<SystemMessageListInfoVo> list=new ArrayList<>();
        /*UserDTO userDTO = UserUtil.getUserInfo();
        PageHelper.startPage(pageQueryDto.getPageNum(), pageQueryDto.getLength());
        List<SystemMessageListInfoVo> list = caseSystemMessageMapper.getSystemMessageList(String.valueOf(userDTO.getUserId()), userDTO.getDeptCode());
        for (SystemMessageListInfoVo systemMessageListInfoVo : list) {
            LocalDateTime localDateTime = systemMessageListInfoVo.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            systemMessageListInfoVo.setMessageExistTime(DateUtil.calculateTimeIntervalInfoSimple(localDateTime, LocalDateTime.now()));
        }*/
        return list;
    }

    /**
     * 新增消息提醒
     *
     * @param systemMessageInsertDto 消息提醒新增Dto
     * @return com.icinfo.framework.common.web.Result<java.lang.String>
     * @author yangsizhen
     * @date 2022/12/09
     */
    @Override
    public void insertSystemMessage(SystemMessageInsertDto systemMessageInsertDto) {
        Date date = new Date();
        CaseSystemMessage caseSystemMessage = new CaseSystemMessage();
        BeanUtil.copyProperties(systemMessageInsertDto, caseSystemMessage);
        caseSystemMessage.setCreateTime(date);
        caseSystemMessage.setId(StringUtils.uuid());
        caseSystemMessage.setIsDeleted("0");
        caseSystemMessage.setIsRead("0");
        caseSystemMessage.setIsSend("0");
        try {
            caseSystemMessageMapper.insertSelective(caseSystemMessage);
        } catch (Exception e) {
            log.warn("新增消息提醒失败:" + e.getMessage());
        }
    }

    /**
     * 消息标记已读
     *
     * @param systemMessageIdRequestDto 消息提醒id请求Dto
     * @return com.icinfo.framework.common.web.Result<java.lang.String>
     * @author yangsizhen
     * @date 2022/12/09
     */
    @Override
    public Result<String> updateSystemMessageRead(SystemMessageIdRequestDto systemMessageIdRequestDto) {
        CaseSystemMessage caseSystemMessage = new CaseSystemMessage();
        caseSystemMessage.setId(systemMessageIdRequestDto.getId());
        caseSystemMessage.setIsRead("1");
        try {
            if (StringUtils.isBlank(systemMessageIdRequestDto.getId())) {
                UserDTO userDTO = UserUtil.getUserInfo();
                caseSystemMessageMapper.updateSystemMessageAllRead(String.valueOf(userDTO.getUserId()), userDTO.getDeptCode());
            } else {
                caseSystemMessageMapper.updateByPrimaryKeySelective(caseSystemMessage);
            }
        } catch (Exception e) {
            log.warn("消息标记已读失败:" + e.getMessage());
            return Result.error("已读失败");
        }
        return Result.success("已读成功");
    }

    /**
     * 查询详细未读数量
     *
     * @return com.icinfo.framework.common.web.Result<java.lang.Integer>
     * @author yangsizhen
     * @date 2022/12/09
     */
    @Override
    public Result<Integer> getNoReadMessageCount() {
        Integer count=0;
        /*UserDTO userDTO = UserUtil.getUserInfo();
        Integer count;
        try {
            count = caseSystemMessageMapper.getNoReadMessageCount(String.valueOf(userDTO.getUserId()), userDTO.getDeptCode());
        } catch (Exception e) {
            log.warn("查询详细未读数量失败:" + e.getMessage());
            return Result.error("查询失败");
        }*/
        return Result.success("查询成功", count);
    }

    /**
     * 查询当前用户最新未读消息
     *
     * @return com.icinfo.framework.common.web.Result<com.icinfo.cloud.provider.punish.common.systemmessage.vo.SystemMessageListInfoVo>
     * @author yangsizhen
     * @date 2022/12/12
     */
    @Override
    public Result<SystemMessageListInfoVo> getNoReadMessageFirstInfo() {
        /*UserDTO userDTO = UserUtil.getUserInfo();
        SystemMessageListInfoVo noReadMessageFirstInfo;
        try {
            noReadMessageFirstInfo = caseSystemMessageMapper.getNoReadMessageFirstInfo(String.valueOf(userDTO.getUserId()), userDTO.getDeptCode());
            if(ObjectUtil.isNotEmpty(noReadMessageFirstInfo)) {
                if(Constant.STRING_1.equals(noReadMessageFirstInfo.getIsSend())) {
                    noReadMessageFirstInfo = null;
                } else {
                    CaseSystemMessage caseSystemMessage = new CaseSystemMessage();
                    caseSystemMessage.setId(noReadMessageFirstInfo.getId());
                    caseSystemMessage.setIsSend("1");
                    caseSystemMessageMapper.updateByPrimaryKeySelective(caseSystemMessage);
                }
            }
        } catch (Exception e) {
            log.warn("查询当前用户最新未读消息失败:" + e.getMessage());
            return Result.error("查询失败");
        }*/
        //SystemMessageListInfoVo noReadMessageFirstInfo=new SystemMessageListInfoVo();
        return Result.success("查询成功", null);
    }
}